public class 正则表达式 {
    public boolean isMatch(String ss, String pp) {
        int n1 = ss.length();
        int n2 = pp.length();
        ss = " " + ss;
        pp = " " + pp;
        char[] s = ss.toCharArray();
        char[] p = pp.toCharArray();
        boolean[][] dp = new boolean[n1 + 1][n2 + 1];
        dp[0][0] = true;
        for(int i = 1;i <= n2;i++){
            if(p[i] == '*'){
                dp[0][i] = dp[0][i - 2];
            }
        }
        for(int i = 1;i <= n1;i++){
            for(int j = 1;j <= n2;j++){
                if(p[j] == '.'){
                    dp[i][j] = dp[i - 1][j - 1];
                }else if(p[j] == '*'){
                    if(p[j - 1] == '.'){
                        dp[i][j] = dp[i - 1][j] || dp[i][j - 2];
                    }else{
                        dp[i][j] = dp[i][j - 2] ||(p[j - 1] == s[i] && dp[i - 1][j]);
                    }
                }else{
                    dp[i][j] = (s[i] == p[j]) && dp[i - 1][j - 1];
                }
            }
        }
        return dp[n1][n2];
    }
}
